
#include <QPainter>
#include <QColorDialog>
#include <QGridLayout>

#include "colorpanel.h"

CColorButton::CColorButton(const QColor& color, bool showColorDialog, QWidget *parent)
    : QToolButton(parent)
    , m_color(color)
    , m_bShowColorDialog(showColorDialog)
    , m_size(size())
    , m_cursor(Qt::ArrowCursor)
{
    Init();
}

CColorButton::~CColorButton()
{

}

void CColorButton::SetColor(const QColor &color)
{
    m_color = color;
    update(rect());
}


const QColor &CColorButton::GetColor() const
{
    return m_color;
}

//设置背景颜色
void CColorButton::setBgColor(QColor color)
{
    emit colorChange(color);
}

void CColorButton::paintEvent(QPaintEvent *event)
{
    //QToolButton::paintEvent(event);
    QPainter painter(this);

    painter.save();
    QRect bgRect = rect().adjusted(1, 1, -1, -1); // 留1px边距
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(m_color));
    //paint.setBrush(brush);
    painter.drawRoundedRect(bgRect, 6, 6);
    //painter.fillRect(rect(), QBrush(m_color));
    painter.restore();

    if (m_bShowColorDialog)
    {
        static const QColor loopGrandientColor[] = {
            Qt::red,
            Qt::green,
            Qt::gray,
            Qt::blue,
            Qt::cyan,
            Qt::magenta,
            Qt::yellow,
            Qt::black,
            Qt::white,
            Qt::darkRed
        };
        static int curColor = 0;
        QWidget::paintEvent(event);

    //    QRadialGradient linearGradient(rect().center(), rect().width()/2, rect().topLeft());
    //    QConicalGradient linearGradient(rect().center(), 0);
        QLinearGradient linearGradient(rect().topLeft(), rect().bottomRight());
        for (int i=0; i<=10; ++i)
        {
            int nColorSize = sizeof(loopGrandientColor)/sizeof(QColor);
            if (curColor >= nColorSize)
            {
                curColor = 0;
            }
            linearGradient.setColorAt(0.1*i, loopGrandientColor[curColor++]);
        }
        QPainterPath path;
        QPolygonF polygonF;
        polygonF << QPointF((rect().bottomLeft().x()+rect().bottomRight().x())/2, rect().bottom()) \
                 << QPointF(rect().bottomRight().x(), rect().bottom()) \
                 << QPointF(rect().right(), (rect().topRight().y()+rect().bottomRight().y())/2);
        path.addPolygon(polygonF);

        painter.fillPath(path, QBrush(linearGradient));
        curColor++;
    }
}

void CColorButton::enterEvent(QEvent *event)
{
    Q_UNUSED(event)
    m_cursor = cursor();
    setCursor(Qt::ArrowCursor);
}

void CColorButton::leaveEvent(QEvent *event)
{
    Q_UNUSED(event)
    setCursor(m_cursor);
}

void CColorButton::OnClick()
{
    if (m_bShowColorDialog)
    {
        QColor color(m_color);
        QColorDialog colorDialog(color);
        //设置option
        colorDialog.setWindowTitle(tr("颜色对话框"));
        colorDialog.setOption(QColorDialog::DontUseNativeDialog);
        colorDialog.setWindowFlag(Qt::WindowStaysOnTopHint);
        colorDialog.setAttribute(Qt::WA_QuitOnClose, false);

        if(colorDialog.exec() == QDialog::Accepted)
        {
            //获取当前选中的颜色
            color = colorDialog.currentColor();
        }
        if (!color.isValid())
            return;

        SetColor(color);
        emit colorChange(color);
        return;
    }

    emit colorChange(m_color);
}

void CColorButton::Init()
{
    SetColor(m_color);
    emit colorChange(m_color);
    connect(this, SIGNAL(clicked()), this, SLOT(OnClick()));
}

CColorPanel::CColorPanel(QWidget *parent)
    : QWidget(parent)
    , m_MainColorButton(QColor(Qt::red), true, this)
{
    Init();
}

void CColorPanel::SmallColorSel(const QColor &color)
{
    if (sender() != &m_MainColorButton)
    {
        m_MainColorButton.SetColor(color);
    }
    emit ColorSel(color);
}

void CColorPanel::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(QColor(243, 243, 243)));
    painter.drawRoundedRect(this->rect(), 10, 10);

    QWidget::paintEvent(event);
}

void CColorPanel::Init()
{
    setObjectName("CColorPanel");
    QGridLayout *pLayout = new(std::nothrow) QGridLayout(this);
    if (!pLayout)
        return;

    pLayout->setContentsMargins(4, 4, 4, 4);
    // 添加大颜色按钮
    m_MainColorButton.SetSize(QSize(10, 10));
    m_MainColorButton.setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
    connect(&m_MainColorButton, &CColorButton::colorChange, this, &CColorPanel::SmallColorSel);
    pLayout->addWidget(&m_MainColorButton, 0, 0, 1, 1);
    const QColor loopGrandientColor[] = {
        Qt::white,
        Qt::black,
        Qt::gray,
        Qt::red,
        Qt::green,
        Qt::blue,
        Qt::yellow,
        Qt::darkRed,
        Qt::darkGreen,
        Qt::darkBlue
    };
    int nCount = sizeof(loopGrandientColor)/sizeof(QColor);
    int nRow = 0;
    int nCol = 1;
    for (int i=1; i<nCount; ++i)
    {
        if (nCount/2 == i)
        {
            nRow++;
            nCol = 0;
        }
        CColorButton *pSmallColorButton = new(std::nothrow) CColorButton(QColor(loopGrandientColor[i]), false, this);
        if (pSmallColorButton)
        {
            pSmallColorButton->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
            pSmallColorButton->SetSize(QSize(10, 10));
            connect(pSmallColorButton, &CColorButton::colorChange, this, &CColorPanel::SmallColorSel);
            pLayout->addWidget(pSmallColorButton, nRow, nCol++, 1, 1);
        }
    }
    setFixedSize(152, 60);
}
